package com.itheima.tanhua.vo;

import cn.hutool.core.collection.CollUtil;
import com.itheima.tanhua.domain.Answer;
import com.itheima.tanhua.domain.Problems;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Lff
 * @date 2022/4/8
 * 问题结果Vo
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProblemsVo {
    private String id;//题目编号
    private String question;//题目内容
    private List<AnswerVo> options;//答案 以及答案id

    public static ProblemsVo init(Problems problems, List<Answer> answers) {
        ProblemsVo pv = new ProblemsVo();
        pv.setId(problems.getId().toString());
        pv.setQuestion(problems.getContent());
        if (CollUtil.isEmpty(answers)) {
            return pv;
        }
        List<AnswerVo> answerVos = new ArrayList<>();
        answers.forEach(answer -> {
            AnswerVo init = AnswerVo.init(answer);
            answerVos.add(init);
        });
        //打乱答案顺序
        Collections.shuffle(answerVos);
        //设置AnswerVo
        pv.setOptions(answerVos);
        return pv;
    }
}
